import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shazam_kit/flutter_shazam_kit.dart';
import 'package:flutter_shazam_kit_example/MusicManager.dart';

///use for android only
const developerToken =
    "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlM3V1Q5UEs3VTUifQ.eyJpYXQiOjE3MDQzMzM4NDYsImV4cCI6MTcwOTUxNzg0NiwiaXNzIjoiUTQ4TDYzWjdMOCJ9.gszCaA9vjwZ4YQjA_l1sCzPO2ugcb3AUZmAl9oyO7qTGZW0GQKbcGT41-TuYRB8CmJ1I1ECjmlIik5sq6-q_kQ";

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _flutterShazamKitPlugin = FlutterShazamKit();
  DetectState _state = DetectState.none;
  final List<MediaItem> _mediaItems = [];

  @override
  void initState() {
    super.initState();
    _flutterShazamKitPlugin.configureShazamKitSession(developerToken: developerToken).then((value) {
      _flutterShazamKitPlugin.onMatchResultDiscovered((result) {
        if (result is Matched) {
          setState(() {
            _mediaItems.insertAll(0, result.mediaItems);
          });
        } else if (result is NoMatch) {
          // do something in no match case
        }
        _flutterShazamKitPlugin.endDetectionWithMicrophone();
      });
      _flutterShazamKitPlugin.onDetectStateChanged((state) {
        setState(() {
          _state = state;
        });
      });
      _flutterShazamKitPlugin.onError((error) {
        print(error.message);
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    _flutterShazamKitPlugin.endSession();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: _body()),
    );
  }

  Widget _body() {
    final theme = Theme.of(context);
    return Column(
      children: [_detectButtonPlay(theme), _detectButton(theme), Expanded(child: _detectedItems(theme))],
    );
  }

  Widget _detectButtonPlay(ThemeData theme) {
    return CupertinoButton(
        child: Container(
          padding: const EdgeInsets.all(14),
          decoration:
          BoxDecoration(color: theme.colorScheme.primary, borderRadius: BorderRadius.circular(10)),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                "播放音乐",
                style: TextStyle(color: Colors.red),
              ),
            ],
          ),
        ),
        onPressed: () async {
          MusicManager().setPlayerUrl("https://dl.espressif.com/dl/audio/ff-16b-2c-44100hz.mp3", "https://dl.espressif.com/dl/audio/ff-16b-2c-44100hz.mp3");
        });
  }

  Widget _detectButton(ThemeData theme) {
    return CupertinoButton(
        child: Container(
          padding: const EdgeInsets.all(14),
          decoration:
              BoxDecoration(color: theme.colorScheme.primary, borderRadius: BorderRadius.circular(10)),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                _state == DetectState.none ? "Start Detect" : "End Detect",
              ),
              if (_state == DetectState.detecting) ...[
                const SizedBox(width: 10),
                const SizedBox(
                  height: 10,
                  width: 10,
                  child: CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(Colors.white)),
                )
              ]
            ],
          ),
        ),
        onPressed: () async {
          if (_state == DetectState.detecting) {
            endDetect();
          } else {
            await MusicManager().setPlayerByPageChange();
            startDetect();
          }
        });
  }

  Widget _detectedItems(ThemeData theme) {
    return ListView.builder(
        itemCount: _mediaItems.length,
        shrinkWrap: true,
        itemBuilder: ((context, index) {
          MediaItem item = _mediaItems[index];
          return Container(
            margin: const EdgeInsets.all(8),
            decoration:
                BoxDecoration(color: const Color(0xFFF5f5f5), borderRadius: BorderRadius.circular(10)),
            child: Row(
              children: [
                Container(
                  decoration: BoxDecoration(borderRadius: BorderRadius.circular(10)),
                  height: 100,
                  width: 100,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(8.0),
                    child: Image.network(
                      item.artworkUrl,
                      height: 150.0,
                      width: 100.0,
                    ),
                  ),
                ),
                const SizedBox(width: 10),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        Text(item.title),
                        Text("Artist: ${item.artist}"),
                        Text("Genres: ${item.genres.join(", ")}")
                      ],
                    ),
                  ),
                )
              ],
            ),
          );
        }));
  }

  startDetect() {
    _flutterShazamKitPlugin.startDetectionWithMicrophone();
  }

  endDetect() {
    _flutterShazamKitPlugin.endDetectionWithMicrophone();
  }
}
